package com.hll.spring.mybatis.service;

import com.hll.spring.mybatis.mapper.UserMapper;
import com.hll.spring.mybatis.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author hll
 * @create 2022-05-11 16:34
 */
@Service
@Transactional(
    readOnly = false,
    timeout = -1,
    propagation = Propagation.REQUIRED,
    isolation = Isolation.REPEATABLE_READ) // 加在类上，表示所有方法都带上这个
public class UserService {
  @Autowired private UserMapper userMapper;

  // 如果没有事务，那么异常之前的处理都生效
  public void updateSomeThing() {
    User user = userMapper.selectById(2); // 2,杰克,18,1
    System.out.println(user);
    user.setAge(20);
    userMapper.updateById(user);
    System.out.println(user);
    int i = 10 / 0;
    user.setAge(30);
    userMapper.updateById(user);
    System.out.println(user);
  }
}
